package se.hh.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import se.hh.lab.dao.UserLoginDao;
import se.hh.lab.dao.impl.UserLoginDaoImpl;
import se.hh.lab.domain.Login;

public class UserChangePassword extends HttpServlet
{


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		HttpSession session=request.getSession();
		Login currentUser=(Login)session.getAttribute("userlogin");
		if(currentUser == null)
		{
			response.sendRedirect("user_login.jsp");
			return;
		}
		String password=request.getParameter("password");
		String repassword=request.getParameter("repassword");
		System.out.println(password);
		System.out.println(repassword);
		System.out.println(currentUser.getUserID());
		if(!password.equals(repassword))
		{
			response.getWriter().write("<script>alert('information is illegal');window.location.href='/lab_user/user_changePassword.jsp'</script>");
			return;
		}
		
		
		Login login=new Login();
		login.setUserID(currentUser.getUserID());
		login.setPassword(password);
		
		UserLoginDao userlogin=new UserLoginDaoImpl();
		if(userlogin.updateUserLogin(login)==1)
		{
			session.setAttribute("userlogin", login);
			response.getWriter().write("<script>alert('success to change password');window.location.href='user_login.jsp'</script>");
		}
		else
		{
			response.getWriter().write("<script>alert('fail to change password');window.location.href='user_login.jsp'</script>");
		}
		
	}

}
